<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/12
 * Time: 下午11:39
 */

namespace Home\Model;
use Think\Model;

class MissionModel extends Model {
    protected $tableName = 'mission';
    protected $pk        = 'mission_id';

    //根据查询条件获取接活首页信息-未登陆
    public function getMInfo($jobs,$duration){
        //根据查询条件获取任务基本信息
        $mission_field = array('mission_id','mission_name','jobs','salary1','salary2','location1','location2','publish_time','publisher_id','duration');
        $mission_order = '`totop` desc,`publish_time` desc';
        $job_arr = explode(",",$jobs);
        for($i=0;$i < count($job_arr);$i++){
            $job_arr[$i] = '%'.$job_arr[$i].'%';
        }
        $duration_arr = explode(",",$duration);
        for($i=0;$i < count($duration_arr);$i++){
            $duration_arr[$i] = '%'.$duration_arr[$i].'%';
        }
        $mission_map['jobs'] = array('like',$job_arr,'or');
        $mission_map['duration'] = array('like',$duration_arr,'or');
        $mission_map['status'] = array('eq',1);
        //dump($user_ext_map);
        $mission_result = $this->field($mission_field)->order($mission_order)->where($mission_map)->select();
        //dump($mission_result);
        if(is_array($mission_result)){
            foreach($mission_result as &$mission_result_one){
                $user_ext_field = array('trust','level_j');
                $user_ext_map['user_id'] = array('eq',$mission_result_one['publisher_id']);
                //dump($user_ext_map);
                $user_ext_db = M('User_ext');
                $user_ext_info = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
                //dump($user_ext_info);
                if(is_array($user_ext_info)){
                    $mission_result_one['level_j'] = $user_ext_info[0]['level_j'];
                }else{
                    //user_ext获取甲方等级失败
                }
            }
        }else{
            //mission表获取数据失败
            //return 0;
        }
        //dump($mission_result);
        return $mission_result;

    }

    //根据查询条件获取接活首页信息－登陆
    public function getMInfoLogin($keyword,$jobs,$duration){
        $mission_field = array('mission_id','mission_name','jobs','salary1','salary2','location1','location2','publish_time','publisher_id','publisher_name','duration','agent');
        $mission_order = '`publish_time` desc';
        //关键字查询
        $mission_map_sub['publisher_name'] = array('like','%'.$keyword.'%');
        $mission_map_sub['mission_name'] = array('like','%'.$keyword.'%');
        $mission_map_sub['agent'] = array('like','%'.$keyword.'%');
        $mission_map_sub['location2'] = array('like','%'.$keyword.'%');
        $mission_map_sub['salary2'] = array('like','%'.$keyword.'%');
        $mission_map_sub['_logic'] = 'or';
        $mission_map['_complex'] = $mission_map_sub;

        $job_arr = explode(",",$jobs);
        for($i=0;$i < count($job_arr);$i++){
            $job_arr[$i] = '%'.$job_arr[$i].'%';
        }
        $duration_arr = explode(",",$duration);
        for($i=0;$i < count($duration_arr);$i++){
            $duration_arr[$i] = '%'.$duration_arr[$i].'%';
        }
        $mission_map['jobs'] = array('like',$job_arr,'or');
        $mission_map['duration'] = array('like',$duration_arr,'or');
        $mission_map['status'] = array('eq',1);
//        dump($mission_map);
        $mission_result = $this->field($mission_field)->order($mission_order)->where($mission_map)->select();
        //dump($mission_result);
        if(is_array($mission_result)){
            foreach($mission_result as &$mission_result_one){
                $user_ext_field = array('trust','level_j');
                $user_ext_map['user_id'] = array('eq',$mission_result_one['publisher_id']);
                //dump($user_ext_map);
                $user_ext_db = M('User_ext');
                $user_ext_info = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
                //dump($user_ext_info);
                if(is_array($user_ext_info)){
                    $mission_result_one['level_j'] = $user_ext_info[0]['level_j'];
                    $mission_result_one['trust'] = $user_ext_info[0]['trust'];
                }else{
                    //user_ext获取甲方等级失败
                }
            }
        }else{
            //mission表获取数据失败
            //return 0;
        }
        //dump($mission_result);
        return $mission_result;
    }

    //根据查询条件获取接活首页信息－登陆 添加分页信息
    public function getMInfoLoginPage($keyword,$jobs,$duration,$page,$row){
        $mission_field = array('mission_id','mission_name','jobs','salary1','salary2','location1','location2','publish_time','publisher_id','publisher_name','duration','agent');
        $mission_order = '`publish_time` desc';
        //关键字查询
        $mission_map_sub['publisher_name'] = array('like','%'.$keyword.'%');
        $mission_map_sub['mission_name'] = array('like','%'.$keyword.'%');
        $mission_map_sub['agent'] = array('like','%'.$keyword.'%');
        $mission_map_sub['location2'] = array('like','%'.$keyword.'%');
        $mission_map_sub['salary2'] = array('like','%'.$keyword.'%');
        $mission_map_sub['_logic'] = 'or';
        $mission_map['_complex'] = $mission_map_sub;

        $job_arr = explode(",",$jobs);
        for($i=0;$i < count($job_arr);$i++){
            $job_arr[$i] = '%'.$job_arr[$i].'%';
        }
        $duration_arr = explode(",",$duration);
        for($i=0;$i < count($duration_arr);$i++){
            $duration_arr[$i] = '%'.$duration_arr[$i].'%';
        }
        $mission_map['jobs'] = array('like',$job_arr,'or');
        $mission_map['duration'] = array('like',$duration_arr,'or');
        $mission_map['status'] = array('eq',1);
//        dump($mission_map);
        $mission_result = $this->field($mission_field)->order($mission_order)->where($mission_map)->page($page,$row)->select();
        //dump($mission_result);
        if(is_array($mission_result)){
            foreach($mission_result as &$mission_result_one){
                $user_ext_field = array('trust','level_j');
                $user_ext_map['user_id'] = array('eq',$mission_result_one['publisher_id']);
                //dump($user_ext_map);
                $user_ext_db = M('User_ext');
                $user_ext_info = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
                //dump($user_ext_info);
                if(is_array($user_ext_info)){
                    $mission_result_one['level_j'] = $user_ext_info[0]['level_j'];
                    $mission_result_one['trust'] = $user_ext_info[0]['trust'];
                }else{
                    //user_ext获取甲方等级失败
                }
            }
        }else{
            //mission表获取数据失败
            //return 0;
        }
        //dump($mission_result);
        return $mission_result;
    }

    //获取单个任务页面任务信息
    public function getTaskInfo($mission_id,$login){
        $mission_field = array('mission_id','mission_name','jobs','duration','salary1','salary2','location1','location2','description','status','publish_time','isphone_public','publisher_id','publisher_name');
        $mission_map['mission_id'] = array('eq',$mission_id);
        $mission_result = $this->field($mission_field)->where($mission_map)->select();
        if(is_array($mission_result)){
            if(1 == $login){
                foreach($mission_result as &$mission_result_one){
                    //user_ext表
                    $user_ext_field = array('trust','level_j','level_y','agent_j','agent_j_desc','ispublic_phone');
                    $user_ext_map['user_id'] = array('eq',$mission_result_one['publisher_id']);
                    $user_ext_db = M('User_ext');
                    $user_ext_result = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
                    if(is_array($user_ext_result)){
                        $mission_result_one['publisher'] = $user_ext_result[0];
                    }else{
                        //获取user_ext表失败
                    }

//                    //电话是否可用
//                    if($user_ext_result[0]['ispublic_phone'] == 1 ){
//                        //电话公开
//                        $mission_result_one['public_phone'] = 1;
//                    }else{
//                        //电话不公开
//                        $mission_result_one['public_phone'] = 2;
//                    }

                    //获取头像user表
                    $user_field = array('user_id','user_name','image_url');
                    $user_map['user_id'] = array('eq',$mission_result_one['publisher_id']);
                    $user_db = M('User');
                    $user_result = $user_db->field($user_field)->where($user_map)->select();
                    if(is_array($user_result)){
                        $mission_result_one['publisher']['user_id'] = $user_result[0]['user_id'];
                        $mission_result_one['publisher']['user_name'] = $user_result[0]['user_name'];
                        $mission_result_one['publisher']['image_url'] = $user_result[0]['image_url'];
                    }else{
                        //获取user表失败
                    }

                    //获取收藏状态
                    $collect_db = M('Collect');
                    $collect_map['collect_type'] = 2;
                    $collect_map['collecter_id'] = $_SESSION['loginUser']['user_id'];
                    $collect_map['collected_id'] = $mission_id;
                    $collect_exist = $collect_db->where($collect_map)->select();
                    if(is_array($collect_exist)){
                        $mission_result_one['collect_status'] = 2;
                    }else{
                        $mission_result_one['collect_status'] = 1;
                    }

                    //miss_user_relate－是否可以一键接活
                    $miss_user_relate_db = M('MissUserRelate');
                    $miss_user_relate_map['mission_id'] = array('eq',$mission_id);
                    $miss_user_relate_map['follower_id'] = array('eq',$_SESSION['loginUser']['user_id']);
                    $miss_user_relate_result = $miss_user_relate_db->where($miss_user_relate_map)->select();
                    //dump($miss_user_relate_result);
                    if(is_array($miss_user_relate_result)){
                        //不可以一键接活
                        if(1 == $miss_user_relate_result[0]['is_selected']){
                            //已申请
                            $mission_result_one['isReceiveMission'] = 2;
                        }elseif( 4 == $miss_user_relate_result[0]['is_selected'] ){
                            //已结束
                            $mission_result_one['isReceiveMission'] = 4;
                        }else{
                            //已接活
                            $mission_result_one['isReceiveMission'] = 3;
                        }
                    }else{
                        //可以一键接活
                        $mission_result_one['isReceiveMission'] = 1;
                    }


                    //若为本人发的活，添加申请者与被选中者信息
                    //*******************************************************************************
                    if( $_SESSION['loginUser']['user_id'] == $mission_result_one['publisher_id'] ){
                        //获取申请此任务者信息
                        $miss_user_relate_field = array('follower_id','is_selected');
                        $miss_user_relate_order = '`id` desc';
                        $miss_user_relate_map1['mission_id'] = array('eq',$mission_result_one['mission_id']);
                        $miss_user_relate_map1['is_selected'] = array('eq',1);
                        $miss_user_relate_db = M('Miss_user_relate');
                        $miss_user_relate_result1 = $miss_user_relate_db->field($miss_user_relate_field)->order($miss_user_relate_order)->where($miss_user_relate_map1)->select();
                        //dump($miss_user_relate_result);
                        if(is_array($miss_user_relate_result1)){
                            foreach($miss_user_relate_result1 as &$miss_user_relate_result1_one){
                                //获取名称 头像  user表
                                $user_field = array('user_id','user_name','image_url');
                                $user_map1['user_id'] = array('eq',$miss_user_relate_result1_one['follower_id']);
                                $user_db = M('User');
                                $user_result1 = $user_db->field($user_field)->where($user_map1)->select();
                                if(is_array($user_result1)){
                                    $miss_user_relate_result1_one['user_id'] = $user_result1[0]['user_id'];
                                    $miss_user_relate_result1_one['user_name'] = $user_result1[0]['user_name'];
                                    $miss_user_relate_result1_one['image_url'] = $user_result1[0]['image_url'];
                                }else{
                                    //获取user表失败
                                }

                                //获取等级user_ext表
                                $user_ext_field = array('trust','level_j','level_y');
                                $user_ext_map['user_id'] = array('eq',$miss_user_relate_result1_one['follower_id']);
                                $user_ext_db = M('UserExt');
                                $user_ext_result1 = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
                                if(is_array($user_ext_result1)){
                                    $miss_user_relate_result1_one['trust'] = $user_ext_result1[0]['trust'];
                                    $miss_user_relate_result1_one['level_j'] = $user_ext_result1[0]['level_j'];
                                    $miss_user_relate_result1_one['level_y'] = $user_ext_result1[0]['level_y'];
                                }else{
                                    //获取user_ext表失败
                                }

                                //获取是否被收藏
                                $collect_map['collect_type'] = 1;
                                $collect_map['collecter_id'] = array('eq',$_SESSION['loginUser']['user_id']);
                                $collect_map['collected_id'] = array('eq',$miss_user_relate_result1_one['follower_id']);
                                $collect_db = M('Collect');
                                //dump($miss_user_relate_result1_one);
                                $collect_result1 = $collect_db->where($collect_map)->select();
                                if(is_array($collect_result1)){
                                    $miss_user_relate_result1_one['collect_status'] = 2;
                                }else{
                                    $miss_user_relate_result1_one['collect_status'] = 1;
                                }

                                //获取miss_user_relate留言信息
                                $miss_user_relate_field_next = array('message');
                                $miss_user_relate_map_next['mission_id'] = $mission_id;
                                $miss_user_relate_map_next['follower_id'] = $miss_user_relate_result1_one['user_id'];
                                $miss_user_relate_db_next = M('MissUserRelate');
                                $miss_user_relate_result_next = $miss_user_relate_db_next->field($miss_user_relate_field_next)->where($miss_user_relate_map_next)->select();
                                if(is_array($miss_user_relate_result_next)){
                                    $miss_user_relate_result1_one['message'] = $miss_user_relate_result_next[0]['message'];
                                }else{
                                    //miss_user_relate表获取留言失败
                                }

                            }
                        }else{
                            //获取miss_user_relate表失败
                        }
                        $mission_result_one['follow'] = $miss_user_relate_result1;


                        //获取成功申请此任务者信息
                        $miss_user_relate_field = array('follower_id','is_selected','evaluate');
                        $miss_user_relate_order = '`id` desc';
                        $miss_user_relate_map2['mission_id'] = array('eq',$mission_result_one['mission_id']);
                        $miss_user_relate_map2['is_selected'] = array('between','2,3');
                        $miss_user_relate_db = M('Miss_user_relate');
                        $miss_user_relate_result2 = $miss_user_relate_db->field($miss_user_relate_field)->order($miss_user_relate_order)->where($miss_user_relate_map2)->select();
                        //dump($miss_user_relate_result2);
                        if(is_array($miss_user_relate_result2)){
                            foreach($miss_user_relate_result2 as &$miss_user_relate_result2_one){
                                $user_field = array('user_id','user_name','image_url');
                                $user_map2['user_id'] = array('eq',$miss_user_relate_result2_one['follower_id']);
                                $user_db = M('User');
                                $user_result2 = $user_db->field($user_field)->where($user_map2)->select();
                                //dump($user_result2);
                                if(is_array($user_result2)){
                                    $miss_user_relate_result2_one['user_id'] = $user_result2[0]['user_id'];
                                    $miss_user_relate_result2_one['user_name'] = $user_result2[0]['user_name'];
                                    $miss_user_relate_result2_one['image_url'] = $user_result2[0]['image_url'];
                                }else{
                                    //获取user表失败
                                }

                                //获取等级user_ext表
                                $user_ext_field = array('trust','level_j','level_y');
                                $user_ext_map['user_id'] = array('eq',$miss_user_relate_result2_one['follower_id']);
                                $user_ext_db = M('UserExt');
                                $user_ext_result2 = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
                                if(is_array($user_ext_result2)){
                                    $miss_user_relate_result2_one['trust'] = $user_ext_result2[0]['trust'];
                                    $miss_user_relate_result2_one['level_j'] = $user_ext_result2[0]['level_j'];
                                    $miss_user_relate_result2_one['level_y'] = $user_ext_result2[0]['level_y'];
                                }else{
                                    //获取user_ext表失败
                                }

                                //获取是否被收藏
                                $collect_map2['collect_type'] = 1;
                                $collect_map2['collecter_id'] = array('eq',$_SESSION['loginUser']['user_id']);
                                $collect_map2['collected_id'] = array('eq',$miss_user_relate_result2_one['follower_id']);
                                $collect_db = M('Collect');
                                $collect_result2 = $collect_db->where($collect_map2)->select();
                                //dump($collect_result2);
                                if(is_array($collect_result2)){
                                    $miss_user_relate_result2_one['collect_status'] = 2;
                                }else{
                                    $miss_user_relate_result2_one['collect_status'] = 1;
                                }
                            }
                        }else{
                            //获取miss_user_relate表失败
                        }
                        //dump($miss_user_relate_result2);
                        $mission_result_one['receive'] = $miss_user_relate_result2;


                        //给前端以标志
                        $mission_result_one['is_self'] = 2;
                    }else{
                        $mission_result_one['is_self'] = 1;
                    }
                    //********************************************************************************

                }
            }else{
                foreach($mission_result as &$mission_result_one){
                    $mission_result_one['publisher']  = array();
                    //不可接活，显示一键接活
                    $mission_result_one['isReceiveMission'] = 5;
                }
            }

            return $mission_result[0];
        }else{
            return 0;
        }
    }

    //获取管理页面首页信息
    public function getManageInfo($publisher_id){
        $mission_field = array('mission_id','mission_name','jobs','duration','salary1','salary2','location1','location2','publish_time','status','valid_time','agent');
        $mission_order = '`publish_time` desc';
        $mission_map1['publisher_id']= array('eq',$publisher_id);
        $mission_map1['status'] = array('eq',1);
        $mission_result1 = $this->field($mission_field)->order($mission_order)->where($mission_map1)->select();

        $mission_map2['publisher_id']= array('eq',$publisher_id);
        $mission_map2['status'] = array('eq',2);
        $mission_result2 = $this->field($mission_field)->order($mission_order)->where($mission_map2)->select();

        if(is_array($mission_result1) || is_array($mission_map2)){
            return $mission_result = array($mission_result1,$mission_result2);
        }else{
            return false;
        }
    }

    //获取管理招募中的活1首页信息
    public function getOneManageInfo($mission_id){
        $mission_field = array('mission_id','mission_name','jobs','duration','salary1','salary2','location1','location2','description','publish_time','valid_time','agent','isphone_public','status','publisher_id');
        $mission_map['mission_id'] = array('eq',$mission_id);
        $mission_result = $this->field($mission_field)->where($mission_map)->select();
        if(is_array($mission_result)){
            foreach ($mission_result as &$mission_result_one) {
                //获取申请此任务者信息
                $miss_user_relate_field = array('follower_id','is_selected');
                $miss_user_relate_order = '`id` desc';
                $miss_user_relate_map1['mission_id'] = array('eq',$mission_result_one['mission_id']);
                $miss_user_relate_map1['is_selected'] = array('eq',1);
                $miss_user_relate_db = M('Miss_user_relate');
                $miss_user_relate_result1 = $miss_user_relate_db->field($miss_user_relate_field)->order($miss_user_relate_order)->where($miss_user_relate_map1)->select();
                //dump($miss_user_relate_result);
                if(is_array($miss_user_relate_result1)){
                    foreach($miss_user_relate_result1 as &$miss_user_relate_result1_one){
                        //获取名称 头像  user表
                        $user_field = array('user_id','user_name','image_url');
                        $user_map1['user_id'] = array('eq',$miss_user_relate_result1_one['follower_id']);
                        $user_db = M('User');
                        $user_result1 = $user_db->field($user_field)->where($user_map1)->select();
                        if(is_array($user_result1)){
                            $miss_user_relate_result1_one['user_id'] = $user_result1[0]['user_id'];
                            $miss_user_relate_result1_one['user_name'] = $user_result1[0]['user_name'];
                            $miss_user_relate_result1_one['image_url'] = $user_result1[0]['image_url'];
                        }else{
                            //获取user表失败
                        }

                        //获取等级user_ext表
                        $user_ext_field = array('trust','level_j','level_y');
                        $user_ext_map['user_id'] = array('eq',$miss_user_relate_result1_one['follower_id']);
                        $user_ext_db = M('UserExt');
                        $user_ext_result1 = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
                        if(is_array($user_ext_result1)){
                            $miss_user_relate_result1_one['trust'] = $user_ext_result1[0]['trust'];
                            $miss_user_relate_result1_one['level_j'] = $user_ext_result1[0]['level_j'];
                            $miss_user_relate_result1_one['level_y'] = $user_ext_result1[0]['level_y'];
                        }else{
                            //获取user_ext表失败
                        }

                        //获取是否被收藏
                        $collect_map['collect_type'] = 1;
                        $collect_map['collecter_id'] = array('eq',$_SESSION['loginUser']['user_id']);
                        $collect_map['collected_id'] = array('eq',$miss_user_relate_result1_one['follower_id']);
                        $collect_db = M('Collect');
                        //dump($miss_user_relate_result1_one);
                        $collect_result1 = $collect_db->where($collect_map)->select();
                        if(is_array($collect_result1)){
                            $miss_user_relate_result1_one['collect_status'] = 2;
                        }else{
                            $miss_user_relate_result1_one['collect_status'] = 1;
                        }

                        //获取一键接活留言信息
                        $message_db = M('Message');
                        $message_field = array('content');
                        $message_map['publisher_id'] = array('eq',$user_result1[0]['user_id']);
                        $message_map['receiver_id'] = array('eq',$mission_result_one['publisher_id']);
                        $url = U('Task/index').'/mission/'.$mission_result_one['mission_id'];
                        $message_map['content'] = array('like','%'.$mission_result_one['mission_name'].'!@#$%^&*'.$url);
                        $message_result = $message_db->field($message_field)->where($message_map)->select();
                        if(is_array($message_result)){
                            $content = $message_result[0]['content'];
                            $content_arr = explode("!@#$%^&*",$content);
                            $miss_user_relate_result1_one['message'] = $content_arr[0];
                        }

                    }
                }else{
                    //获取miss_user_relate表失败
                }
                $mission_result_one['follow'] = $miss_user_relate_result1;

                //获取成功申请此任务者信息
                $miss_user_relate_field = array('follower_id','is_selected','evaluate');
                $miss_user_relate_order = '`id` desc';
                $miss_user_relate_map2['mission_id'] = array('eq',$mission_result_one['mission_id']);
                $miss_user_relate_map2['is_selected'] = array('between','2,3');
                $miss_user_relate_db = M('Miss_user_relate');
                $miss_user_relate_result2 = $miss_user_relate_db->field($miss_user_relate_field)->order($miss_user_relate_order)->where($miss_user_relate_map2)->select();
                //dump($miss_user_relate_result2);
                if(is_array($miss_user_relate_result2)){
                    foreach($miss_user_relate_result2 as &$miss_user_relate_result2_one){
                        $user_field = array('user_id','user_name','image_url');
                        $user_map2['user_id'] = array('eq',$miss_user_relate_result2_one['follower_id']);
                        $user_db = M('User');
                        $user_result2 = $user_db->field($user_field)->where($user_map2)->select();
                        //dump($user_result2);
                        if(is_array($user_result2)){
                            $miss_user_relate_result2_one['user_id'] = $user_result2[0]['user_id'];
                            $miss_user_relate_result2_one['user_name'] = $user_result2[0]['user_name'];
                            $miss_user_relate_result2_one['image_url'] = $user_result2[0]['image_url'];
                        }else{
                            //获取user表失败
                        }

                        //获取等级user_ext表
                        $user_ext_field = array('trust','level_j','level_y');
                        $user_ext_map['user_id'] = array('eq',$miss_user_relate_result2_one['follower_id']);
                        $user_ext_db = M('UserExt');
                        $user_ext_result2 = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
                        if(is_array($user_ext_result2)){
                            $miss_user_relate_result2_one['trust'] = $user_ext_result2[0]['trust'];
                            $miss_user_relate_result2_one['level_j'] = $user_ext_result2[0]['level_j'];
                            $miss_user_relate_result2_one['level_y'] = $user_ext_result2[0]['level_y'];
                        }else{
                            //获取user_ext表失败
                        }

                        //获取是否被收藏
                        $collect_map2['collect_type'] = 1;
                        $collect_map2['collecter_id'] = array('eq',$_SESSION['loginUser']['user_id']);
                        $collect_map2['collected_id'] = array('eq',$miss_user_relate_result2_one['follower_id']);
                        $collect_db = M('Collect');
                        $collect_result2 = $collect_db->where($collect_map2)->select();
                        //dump($collect_result2);
                        if(is_array($collect_result2)){
                            $miss_user_relate_result2_one['collect_status'] = 2;
                        }else{
                            $miss_user_relate_result2_one['collect_status'] = 1;
                        }
                    }
                }else{
                    //获取miss_user_relate表失败
                }
                //dump($miss_user_relate_result2);
                $mission_result_one['receive'] = $miss_user_relate_result2;
            }
            return $mission_result[0];
        }else{
            return false;
        }
    }

    //就要他了－获取项目列表
    public function getProjectList($publisher_id){
        $mission_field = array('mission_id','mission_name');
        $mission_order = '`publish_time` desc ';
        $mission_map['publisher_id'] = array('eq',$publisher_id);
        $mission_map['status'] = array('eq',1);
        $mission_result = $this->field($mission_field)->order($mission_order)->where($mission_map)->select();
        //dump($mission_result);
        if(is_array($mission_result)){
            return $mission_result;
        }else{
            return false;
        }
    }

    //获取甲方等级
    public function getLevelJ($user_id){
        $mission_map['publisher_id'] = array('eq',$user_id);
        $mission_map['status'] = array('eq',2);
        $mission_result = $this->where($mission_map)->count();
        if( 0 <= $mission_result && $mission_result <= 3 ){
            return 1;
        }elseif( 4 <= $mission_result && $mission_result <= 15 ){
            return 2;
        }elseif( 16 <= $mission_result && $mission_result <= 30 ){
            return 3;
        }elseif( 31 <= $mission_result && $mission_result <= 60 ){
            return 4;
        }elseif( 61 <= $mission_result && $mission_result <= 100 ){
            return 5;
        }elseif( 101 < $mission_result && $mission_result <= 150 ){
            return 6;
        }elseif( 151 <= $mission_result && $mission_result <=250 ){
            return 7;
        }elseif( 251 <= $mission_result && $mission_result <= 500 ){
            return 8;
        }elseif( 501 <= $mission_result ){
            return 9;
        }else{
            return false;
        }
    }

    //更新任务状态－－根据发布时间和时限
    public function setMissionUpdate(){
        //获取处于发布状态的任务
        $mission_field = array('mission_id','status','first_publish_time','valid_time');
        $mission_map['status'] = array('eq',1);
        $mission_result = $this->field($mission_field)->where($mission_map)->select();
        //dump($mission_result);
        if(is_array($mission_result)){
            foreach($mission_result as $mission_result_one){
                $end_time = date("Y-m-d H:i:s",strtotime("+".$mission_result_one['valid_time']."day",strtotime($mission_result_one['first_publish_time'])));
                if($end_time < date("Y-m-d H:i:s",time())){
                    $data['status'] = 2;
                    $mission_update_map['mission_id'] = array('eq',$mission_result_one['mission_id']);
                    $mission_update_result = $this->where($mission_update_map)->save($data);
                }
            }
        }

    }

} 